package com.module.admin.tts.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.module.admin.BaseController;
import com.module.admin.sys.pojo.SysUser;
import com.module.admin.tts.enums.ServInfoStatus;
import com.ms.client.task.api.pojo.ServInfo;
import com.ms.client.task.api.pojo.TaskJob;
import com.ms.client.task.api.service.ApiServInfoService;
import com.ms.client.task.api.service.ApiTaskJobService;
import com.ms.client.task.api.service.ApiTaskProjectService;
import com.ms.msclient.MsClientResponse;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameJsonUtil;
import com.system.comm.utils.FrameStringUtil;

/**
 * task_project的Controller
 * @author yuejing
 * @date 2015-03-30 14:07:28
 * @version V1.0.0
 */
@Controller
public class TtsTaskJobController extends BaseController {

	@Autowired
	private ApiTaskJobService apiTaskJobService;
	@Autowired
	private ApiTaskProjectService apiTaskProjectService;
	@Autowired
	private ApiServInfoService apiServInfoService;

	/**
	 * 跳转到管理页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ttsTaskJob/f-view/manager")
	public String manger(HttpServletRequest request, ModelMap modelMap) {
		List<ServInfo> servInfos = apiServInfoService.findByStatus(ServInfoStatus.NORMAL.getCode()).getBody();
		modelMap.put("servInfos", servInfos);
		return "admin/tts/task/job-manager";
	}

	@RequestMapping(value = "/ttsTaskJob/f-view/edit")
	public String edit(HttpServletRequest request, ModelMap modelMap, Integer id, Integer projectid) {
		if(id != null) {
			modelMap.put("taskJob", apiTaskJobService.get(id).getBody());
		} else {
			modelMap.put("taskProject", apiTaskProjectService.get(projectid).getBody());
		}
		return "admin/tts/task/job-edit";
	}
	@RequestMapping(value = "/ttsTaskJob/f-view/exp")
	public String exp(HttpServletRequest request, ModelMap modelMap, String ids, Integer projectid) {
		List<String> idList = FrameStringUtil.toArray(ids, ",");
		List<TaskJob> list = new ArrayList<TaskJob>();
		for (String item : idList) {
			Integer id = Integer.valueOf(item);
			TaskJob taskJob = apiTaskJobService.get(id).getBody();
			list.add(taskJob);
		}
		modelMap.put("taskJobs", FrameJsonUtil.toString(list));
		return "admin/tts/task/job-exp";
	}
	@RequestMapping(value = "/ttsTaskJob/f-view/imp")
	public String imp(HttpServletRequest request, ModelMap modelMap, Integer projectid) {
		return "admin/tts/task/job-imp";
	}

	@RequestMapping(value = "/ttsTaskJob/f-json/pageQuery")
	@ResponseBody
	public void pageQuery(HttpServletRequest request, HttpServletResponse response, TaskJob taskJob) {
		MsClientResponse<Page<TaskJob>> frame = apiTaskJobService.pageQuery(taskJob);
		writerJson(response, frame);
	}
	@RequestMapping(value = "/ttsTaskJob/f-json/save")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response, TaskJob taskJob) {
		SysUser user = getSessionUser(request);
		taskJob.setAdduser(user.getUserId());
		MsClientResponse<TaskJob> frame = apiTaskJobService.save(taskJob);
		writerJson(response, frame);
	}
	@RequestMapping(value = "/ttsTaskJob/f-json/execJob")
	@ResponseBody
	public void execJob(HttpServletRequest request, HttpServletResponse response, Integer id) {
		MsClientResponse<TaskJob> frame = apiTaskJobService.execJob(id);
		writerJson(response, frame);
	}
	@RequestMapping(value = "/ttsTaskJob/f-json/delete")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response, Integer id) {
		MsClientResponse<TaskJob> frame = apiTaskJobService.delete(id);
		writerJson(response, frame);
	}
	@RequestMapping(value = "/ttsTaskJob/f-json/updateStatus")
	@ResponseBody
	public void updateStatus(HttpServletRequest request, HttpServletResponse response, Integer id, Integer status) {
		MsClientResponse<TaskJob> frame = apiTaskJobService.updateStatus(id, status);
		writerJson(response, frame);
	}
	@RequestMapping(value = "/ttsTaskJob/f-json/imp")
	@ResponseBody
	public void imp(HttpServletRequest request, HttpServletResponse response, Integer projectid, String json) {
		SysUser user = getSessionUser(request);
		MsClientResponse<TaskJob> frame = new MsClientResponse<>();
		List<TaskJob> list = FrameJsonUtil.toList(json, TaskJob.class);
		for (TaskJob taskJob : list) {
			taskJob.setId(null);
			taskJob.setAddtime(null);
			taskJob.setUpdatetime(null);
			taskJob.setProjectid(projectid);
			taskJob.setAdduser(user.getUserId());
			frame = apiTaskJobService.save(taskJob);
		}
		writerJson(response, frame);
	}
}